

//
//  HMAccountTool.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/9.
//  Copyright © 2017年 Mac. All rights reserved.
//

#define HMAccountFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"]


#import "HMAccountTool.h"

@implementation HMAccountTool

//存储账号信息
+ (void)save:(HMAccount *)account{
    //存储模型使用归档
    //前提 模型(HMAccount)应该遵守<NSCoding>协议,并实现代理方法
    //归档
    [NSKeyedArchiver archiveRootObject:account toFile:HMAccountFilePath];
}


//读取账号信息
+ (HMAccount *)account{
    //解档
    HMAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:HMAccountFilePath];
    //判断账号是否过期
    NSDate *now = [NSDate date];
    /*
     //升序:越往右边越大
     {NSOrderedAscending = -1L,
     //相等
     NSOrderedSame, 
     //降序:越往右边越小
     NSOrderedDescending};
     */
    
    if ([now compare:account.expires_time] != NSOrderedAscending) {
        //过期
        account = nil;
    }

    return account;
}

@end
